import { parseLyric } from "./parseLysic"
import request from "../api/request"
import { GetLyric, GetSongUrl, GetSongDetail } from "../api/music"
// GetLyric, GetSongUrl, GetSongDetail 是封装的axios请求

export const actions = {
    // 获取歌曲信息
    async getSong (context, data) {
        const result = await GetSongDetail({
            ids: data.id
        })
        let song = result.songs[0]
        let music = {
            id: data.id,
            name: "歌曲正在加载中...",
            songName: song.name,
            picUrl: song.al.picUrl,
            author: song.ar[0].name,
        }
        let res = await GetSongUrl({
            id: data.id
        })


        //判断是否可以获取到歌曲的播放地址
        if (!res.data[0].url) {
            Vue.prototype.$notify.error({
                title: "错误",
                message: `歌曲 ${music.songName} 是vip歌曲无法获取`
            });
            this.commit("next")
            return;
        }

        // 获取歌词
        let lyric = await GetLyric({
            id: data.id
        })

        if (lyric.lrc) {
            lyric = lyric.lrc.lyric
        } else {
            lyric = "暂无歌词"
        }

        let newlyric = parseLyric(lyric)
        music.src = res.data[0].url
        music.lyric = newlyric

        let autoplay = data.autoplay

        this.commit("changeCanPlay", false) // 更改歌曲是否可以播放的状态为false(不可播放)
        this.commit("upDateSong", { music, autoplay })
    },

    // 获取榜单
    async getMusic () {
        let soaringList = await request({
            url: "/top/list",
            data: {
                idx: "3",
            },
            method: "get",
        })
        let newSongList = await request({
            url: "/top/list",
            data: {
                idx: "0",
            },
            method: "get",
        })
        let hotSongList = await request({
            url: "/top/list",
            data: {
                idx: "1",
            },
            method: "get",
        })
        let theOriginalList = await request({
            url: "/top/list",
            data: {
                idx: "2",
            },
            method: "get",
        })
        let listOfSinger = await request({
            url: "/toplist/artist",
            method: "get",
        })
        // console.log(soaringList, newSongList, hotSongList, theOriginalList, listOfSinger)
        this.commit("getRanking", {
            soaringList: soaringList.playlist.tracks,
            newSongList: newSongList.playlist.tracks,
            hotSongList: hotSongList.playlist.tracks,
            theOriginalList: theOriginalList.playlist.tracks,
            listOfSinger: listOfSinger.list.artists
        })
    },

    // 静默注册登录
    async silentRegistered (context, msg) {
        let res = await request({
            url: "/user/register",
            data: {
                userName: msg.userName,
                passWord: "",
                inviteCode: "",
                photo: msg.photo,
            },
            method: "post",
        })

        let result = await request({
            url: "/user/login",
            data: {
                account: res.account,
                passWord: "",
            },
            method: "post",
        })

        let { token, user } = result
        this.commit("saveToken", token)
        this.commit("saveUser", user)
        window.localStorage.setItem(
            "blogToken",
            JSON.stringify({ token, date: new Date(), isTourists: user.isTourists })
        )
    },
    // socket.io登陆
    SOCKET_USER_INFO ({ state }) {
        state.socket.emit("submit", state.user.account)
    },

    SOCKET_SEND_MSG ({ state }, data) {
        state.socket.emit("sendMsg", data)
    },

    // 注销（断开连接）
    SOCKET_LOGOUT ({ state }) {
        let res = request({
            url: "/logout",
            data: {
                token: state.token
            },
            method: "post"
        })
    },

    // 获取评论通知
    async ObtainNotice ({ state }) {
        let res = await request({
            url: "/notice/obtain",
            data: {
                token: state.token,
            },
            method: "get",
        })
        let comments = []
        res.data.comments.forEach((item) => {
            comments.unshift(item)
        })
        res.data.comments = []
        res.data.comments.push(...comments)
        this.commit("updateNotify", res.data)
    },

    // 获取留言通知
    async ObtainInform ({ state }) {
        let res = await request({
            url: "/inform/obtain",
            data: {
                token: state.token,
            },
            method: "get",
        })
        let message = []
        res.data.message.forEach((item) => {
            message.unshift(item)
        })
        res.data.message = []
        res.data.message.push(...message)
        this.commit("updateInform", res.data)
    },

}